package com.weasel.weixin.commons.media;

import com.google.common.collect.ImmutableMap;
import com.weasel.helper.JsonHelper;
import com.weasel.http.HttpClientSecretary;
import com.weasel.http.helper.HttpResponseHelper;
import com.weasel.http.helper.HttpStatusHelper;
import com.weasel.http.helper.HttpUrlHelper;
import com.weasel.weixin.commons.CommonRequest;
import com.weasel.weixin.commons.config.CommonConfig;
import com.weasel.weixin.commons.media.bean.ForeverVideo;
import com.weasel.weixin.commons.media.bean.Media;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.ExecutionException;

/**临时素材请求类
 * Created by Dylan on 2015/9/29.
 */
public abstract class ForeverMediaRequest extends CommonRequest {


    public ForeverMediaRequest(CommonConfig config) {
        super(config);
    }


    public ForeverMediaResponse upload(Media media) throws ExecutionException, IOException {

        String uri = HttpUrlHelper.withURL(getUploadURI()).addParam("access_token",getAccessToken()).addParam("type",media.getType()).build();;
        MultipartEntityBuilder builder = MultipartEntityBuilder.create()
                                                               .setCharset(Consts.UTF_8)
                                                               .setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        builder.addBinaryBody(StringUtils.substringBefore(media.getFile().getName(), "."), media.getFile());
        if(media instanceof ForeverVideo){
            builder.addTextBody("description", JsonHelper.toJsonString(((ForeverVideo) media).getDescription()));
        }
        HttpEntity entity =	builder.build();

        CloseableHttpResponse response = HttpClientSecretary.post()
                                                            .addHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString())
                                                            .withURI(uri)
                                                            .withEntity(entity)
                                                            .doIt();
        ForeverMediaResponse foreverMediaResponse = HttpResponseHelper.jsonResponseToEntity(response, ForeverMediaResponse.class);
        HttpClientSecretary.release(response);
        return  foreverMediaResponse;
    }

    public ForeverMediaResponse download(String mediaId,String fileDirectory) throws ExecutionException, IOException {

        String uri = HttpUrlHelper.withURL(getDownloadURI()).addParam("access_token",getAccessToken()).addParam("media_id",mediaId).build() ;

        CloseableHttpResponse response = HttpClientSecretary.post()
                                                            .withURI(uri)
                                                            .doIt();

        ForeverMediaResponse foreverMediaResponse = HttpResponseHelper.jsonResponseToEntity(response, ForeverMediaResponse.class);
        if(HttpStatusHelper.isOK(response.getStatusLine().getStatusCode())){
            HttpEntity entity = response.getEntity();
            InputStream is = entity.getContent();
            FileUtils.copyInputStreamToFile(is, new File(fileDirectory+getFileName(response)));
            is.close();
        }
        HttpClientSecretary.release(response);
        return  foreverMediaResponse;
    }

    public ForeverMediaResponse delete(String mediaId) throws ExecutionException, IOException {

        String uri = HttpUrlHelper.withURL(getDeleteURI()).addParam("access_token",getAccessToken()).addParam("media_id",mediaId).build() ;

        CloseableHttpResponse response = HttpClientSecretary.post()
                                                            .withURI(uri)
                                                            .doIt();
        ForeverMediaResponse foreverMediaResponse = HttpResponseHelper.jsonResponseToEntity(response, ForeverMediaResponse.class);
        HttpClientSecretary.release(response);
        return foreverMediaResponse;
    }

    public ForeverMediaCountResponse count() throws ExecutionException, IOException {

        CloseableHttpResponse response = HttpClientSecretary.get()
                                                            .withURI(getBaseUrl(getCountURL()))
                                                            .doIt();
        ForeverMediaCountResponse foreverMediaCountResponse = HttpResponseHelper.jsonResponseToEntity(response, ForeverMediaCountResponse.class);
        return  foreverMediaCountResponse;
    }

    public ForeverMediaListResponse list(Media type,int offset,int count) throws ExecutionException, IOException {

        CloseableHttpResponse response = HttpClientSecretary.post()
                                                            .withURI(getBaseUrl(getListURL()))
                                                            .withParams(ImmutableMap.of("type",type.getType(),"offset", String.valueOf(offset), "count", String.valueOf(count)))
                                                            .doIt();
        ForeverMediaListResponse foreverMediaListResponse = HttpResponseHelper.jsonResponseToEntity(response, ForeverMediaListResponse.class);
        return foreverMediaListResponse;
    }

    protected abstract String getUploadURI();

    protected abstract String getDownloadURI();

    protected abstract String getDeleteURI();

    protected abstract String getCountURL();

    protected abstract String getListURL();

}
